package com.cx.service.impl;

import com.cx.mapper.UserInfoMapper;
import com.cx.service.MemberService;
import com.cx.vo.MemberView;
import com.cx.vo.UserMemberView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cx.pojo.Member;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/**
 * @Description GroupOneproject-IntelliJ IDEA
 * @Author Pyc
 * @Date 2020/5/13 16:19
 * @Version 1.0
 */
@Service
public class MemberServiceImpl implements MemberService {

    @Autowired
    UserInfoMapper userInfoMapper;

    @Override
    public List<MemberView> getAllMemberType() {
        List<Member> members = userInfoMapper.selectAllMember();
        List<MemberView> memberViews = new ArrayList<>();

        for (Member member:members) {
            MemberView memberView = new MemberView();
            memberView.setLevel(member.getLevel());
            memberView.setMemberName(member.getMemberName());
            memberView.setMemberReward(member.getMemberReward());

            if (member.getMemberCheckout()<1){
                memberView.setMemberCheckout("无权限");
            }else if (member.getMemberCheckout()>1){
                memberView.setMemberCheckout("延长3小时");
            }else
                memberView.setMemberCheckout("延长1小时");

            if (member.getMemberDeny()<1){
                memberView.setMemberDeny("无权限");
            }else if (member.getMemberDeny()>1){
                memberView.setMemberDeny("保留3小时");
            }else
                memberView.setMemberDeny("保留1小时");

            memberView.setMemberDiscount((int)(member.getMemberDiscount()*100)==100?"无折扣":(int)(member.getMemberDiscount()*100)+"折");

            memberView.setMemberExpensedis((int)(member.getMemberExpensedis()*100)==100?"无折扣":(int)(member.getMemberExpensedis()*100)+"折");

            System.out.println(memberView);
            memberViews.add(memberView);
        }

        memberViews.sort(new Comparator<MemberView>() {
            @Override
            public int compare(MemberView o1, MemberView o2) {
                return o1.getLevel()-o2.getLevel();
            }
        });

        memberViews.forEach(System.out::println);
        return memberViews;
    }

    @Override
    public UserMemberView getMemberBy(UserMemberView userMemberView) {

        String Name = userInfoMapper.selectMemberNameByLevel(userMemberView.getLevel());
        userMemberView.setMemberName(Name);
        Integer nextLevelReward = userInfoMapper.selectNextRewardByLevel(userMemberView.getLevel());
        if (nextLevelReward!=null){
            userMemberView.setRemainReward(nextLevelReward-userMemberView.getMemberReward());
        }else
            userMemberView.setRemainReward(0);


        return userMemberView;
    }
}
